package rest

import (
	"errors"
	"strconv"

	"github.com/labstack/echo/v4"

	"bmember/pkg/lueerror"

	pb "bmember/api/pb/server"
	"bmember/internal/service"
)

type cHandler struct {
	*Base
	name string
}

// NewCardHandler 会员体系相关
func NewCardHandler() *cHandler {
	return &cHandler{
		Base: base,
		name: "card_handler",
	}
}

// Register 注册路由
func (hl *cHandler) Register(r *echo.Group) {
	// 创建卡
	r.POST("/v1/create", hl.createCard)
	// 更新等级
	r.PUT("/v1/update", hl.updateCard)
	// 获取卡详情
	r.GET("/v1/:card_id", hl.getCardById)
	// 获取体系下卡定义
	r.GET("/v1/list", hl.getCards)
	// 设置会员卡默认状态
	r.PUT("/v1/default/set", hl.setCardDefault)
	// 会员卡下拉列表
	r.GET("/v1/selection", hl.cardsSelection)
	// 判断品牌下是否有卡
	r.GET("/v1/group/check", hl.brandCardsCheck)
}

// 创建等级
func (hl *cHandler) createCard(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	req := &pb.CreateCardReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	ret, errL := service.GetCardSrv().CreatAccountCard(ctx, token.BrandId, req, token.EmpId)
	return hl.SimpleRet(c, ret, errL)
}

// 更新等级
func (hl *cHandler) updateCard(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	req := &pb.UpdateCardReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	ret, errL := service.GetCardSrv().UpdateAccountCard(ctx, token.BrandId, req, token.EmpId)
	return hl.SimpleRet(c, ret, errL)
}

// GetCards 获取体系下等级定义
func (hl *cHandler) getCards(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	ret, errL := service.GetCardSrv().GetAccountCardList(ctx, token.BrandId)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *cHandler) getCardById(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	card := c.Param("card_id")
	cardId, err := strconv.ParseInt(card, 10, 64)
	if err != nil {
		return lueerror.InvalidRequest(err)
	}
	ret, errL := service.GetCardSrv().GetCardById(ctx, token.BrandId, cardId)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *cHandler) setCardDefault(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	req := &pb.SetCardDefaultReq{}
	if errL = hl.ParseBody(c, req); errL != nil {
		return errL
	}
	errL = service.GetCardSrv().SetCardDefaultStatus(ctx, token.BrandId, req.CardId, req.IsDefault, token.EmpId)
	return hl.SimpleRet(c, nil, errL)
}

func (hl *cHandler) cardsSelection(c echo.Context) error {
	ctx := c.Request().Context()
	token, errL := hl.AdminPermissionCheck(c, "member")
	if errL != nil {
		return errL
	}
	ret, errL := service.GetCardSrv().GetCardNames(ctx, token.BrandId)
	return hl.SimpleRet(c, ret, errL)
}

func (hl *cHandler) brandCardsCheck(c echo.Context) error {
	ctx := c.Request().Context()
	brandId, errL := parseInt64Query(c, "brand_id")
	if errL != nil || brandId == 0 {
		return lueerror.InvalidRequest(errors.New("品牌 brand_id 不为空"))
	}
	has, errL := service.GetCardSrv().BrandHasCards(ctx, brandId)
	if errL != nil {
		return errL
	}
	ret := map[string]interface{}{"has_cards": has}
	return hl.SimpleRet(c, ret, nil)
}
